package com.lovefreedom.psi.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.lovefreedom.psi.bean.Category;
import com.lovefreedom.psi.bean.Goods;
import com.lovefreedom.psi.dao.GoodsDao;
import com.lovefreedom.psi.util.HibernateSessionFactory;

public class GoodsDaoImpl implements GoodsDao {
	
	@Override
	public void addGoods(Goods goods) {
		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.save(goods);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void updateGoods(Goods goods) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.update(goods);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void deleteGoods(Goods goods) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.delete(goods);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void deleteGoods(int id) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();			
			Goods goods = (Goods)s.get(Goods.class,id);
			s.delete(goods);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}
	@Override
	public Goods findGoodsById(int id) {	

		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			Goods goods = (Goods)s.get(Goods.class,id);
			return goods;
		}finally{
			if(s!=null)
				s.close();
		}
	}
	
	@Override
	public List<Goods> getGoodsList() {	
		List<Goods> list = new ArrayList();
		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			String hql = "from Goods order by id asc";
			Query query = s.createQuery(hql);
			list = query.list(); 
			for(int i=0;i<list.size();i++)
			{
				list.get(i).getCategory().getName();
			}
			return list;
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public List<Goods> getGoodsList(int pageSize, int pageIndex) {	
		List<Goods> list = new ArrayList();
		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			String hql = "from Goods order by id asc";
			Query query = s.createQuery(hql);
			
			query.setFirstResult((pageIndex-1)*pageSize);
			query.setMaxResults(pageSize);			
			list = query.list();
			for(int i=0;i<list.size();i++)
			{
				list.get(i).getCategory().getName();
			}
			
			System.out.println(list.size());
			return list;
		}finally{
			if(s!=null)
				s.close();
		}
	}	

	@Override
	public List<Goods> getGoodsList(int pageSize, int pageIndex, String keywords) {	
		List<Goods> list = new ArrayList();
		Session s = null;
		try{
			String hql = "";
			s = HibernateSessionFactory.getSession();
			if(!keywords.equals("")) {
				hql = "from Goods as goods where goods.code like :code or goods.name like :name order by id asc";
			}else{
				hql = "from Goods order by id asc";
			}				
			Query query = s.createQuery(hql);
			if(!keywords.equals("")) {
				query.setString("code", keywords);				
				query.setString("name", keywords);	
			}

			query.setFirstResult((pageIndex-1)*pageSize);
			query.setMaxResults(pageSize);			
			list = query.list();
			for(int i=0;i<list.size();i++)
			{
				list.get(i).getCategory().getName();
			}
			
			System.out.println(list.size());
			return list;
		}finally{
			if(s!=null)
				s.close();
		}
	}		
	
	@Override
	public int getGoodsTotalCount() {	
		List<Goods> list = new ArrayList();
		int totalCount = 0;
		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			String hql = "from Goods order by id asc";
			Query query = s.createQuery(hql);
			
			totalCount  = query.list().size();
			
			return totalCount;
		}finally{
			if(s!=null)
				s.close();
		}
	}		

	@Override
	public int getGoodsTotalCount(String keywords) {	
		List<Goods> list = new ArrayList();
		int totalCount = 0;
		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			String hql = "";
			if(!keywords.equals("")) {
				hql = "from Goods as goods where goods.code like :code or goods.name like :name order by id asc";
			}else{
				hql = "from Goods order by id asc";
			}			
			Query query = s.createQuery(hql);
			if(!keywords.equals("")) {
				query.setString("code", keywords);
				query.setString("name", keywords);	
			}
			
			totalCount  = query.list().size();
			
			return totalCount;
		}finally{
			if(s!=null)
				s.close();
		}
	}		
	

	@Override
	public List<Goods> getGoodsList(int categoryId){
		List list = new ArrayList();
		return list;
	}

	@Override
	public List<Goods> getGoodsList(String key){
		List list = new ArrayList();
		return list;
	}

	@Override
	public void importGoods(List list){
		
	}

	@Override
	public List<Goods> exportGoods(){
		List list = new ArrayList();
		return list;
	}
}
